'''
[59. 螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/)
'''
from typing import List
'''
[59. 螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/)
'''
from typing import List
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        count = 1
        nums = [[0]*n for _ in range(n)]
        startx,starty = 0,0
        loop,mid = n//2,n//2
        for offset in range(1,loop+1):
            for j in range(startx,n-offset):
                nums[startx][j] = count
                count += 1
            for i in range(starty,n-offset):
                nums[i][j+1] = count
                count += 1
            for x in range(n-offset,startx,-1):
                nums[i+1][x] = count
                count += 1
            for y in range(n-offset,starty,-1):
                nums[y][x-1] = count
                count +=1
            startx +=1
            starty +=1
        if n % 2 != 0:
            nums[mid][mid] = count

        return nums   
        
if __name__ == '__main__':
    s = Solution()
    print(s.generateMatrix(5))
        
           
             

        